import { ref, onMounted } from "vue";
import { listProduct } from "../api/goods";

export function useGoods() {
  const goodsList = ref<any[]>([]);
  const loading = ref(false);
  const error = ref<string | null>(null);

  const fetchGoods = async () => {
    loading.value = true;
    error.value = null;
    try {
      const response = await listProduct({
        pageNum: 1,
        pageSize: 100,
        externalType: 0,
        type: 1,
      });
      goodsList.value = response.rows;
    } catch (err: any) {
      error.value = err.message || "获取商品列表失败";
    } finally {
      loading.value = false;
    }
  };

  onMounted(fetchGoods);

  return {
    goodsList,
    loading,
    error,
    fetchGoods,
  };
}
